package com.itany.netclass.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mvc.annotation.RequestMapping;
import com.itany.mvc.annotation.ResponseBody;
import com.itany.netclass.constant.PageConstant;
import com.itany.netclass.constant.ResponseCodeConstant;
import com.itany.netclass.entity.Resource;
import com.itany.netclass.entity.SearchForm;
import com.itany.netclass.entity.User;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.UserService;
import com.itany.netclass.util.ParameterUtil;
import com.itany.netclass.util.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:21-10-12 下午5:06
 * Version:1.0-SNAPSHOT
 * Description:
 */
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public String login(HttpServletRequest request , HttpServletResponse response){
        UserService userService= (UserService) ObjectFactory.getObject("UserService");
        String loginName = request.getParameter("loginName");
        System.out.println("登录名="+loginName);
        String password = request.getParameter("password");
        System.out.println("密码="+password);
        String code = (String) request.getSession().getAttribute("code");
        String yzm = request.getParameter("yzm");
        System.out.println("输入的验证码="+yzm);
        try {
            User user = userService.login(loginName, password, yzm, code);
            request.getSession().setAttribute("loginUser",user);
            return "redirect:/showHome.do";
        } catch (NullSringException e) {
            e.printStackTrace();
            request.getSession().setAttribute("loginMrg",e.getMessage());
            return "/showLogin.do";
        } catch (CodeException e) {
            e.printStackTrace();
            request.getSession().setAttribute("loginMrg",e.getMessage());
            return "/showLogin.do";
        } catch (NameOrPasswordException e) {
            e.printStackTrace();
            request.getSession().setAttribute("loginMrg",e.getMessage());
            return "/showLogin.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            request.getSession().setAttribute("loginMrg",e.getMessage());
            return "/showLogin.do";
        } catch (NoQuanXianException e) {
            e.printStackTrace();
            request.getSession().setAttribute("loginMrg",e.getMessage());
            return "/showLogin.do";
        }
    }
    @RequestMapping("/exit")
    public String exit(HttpServletRequest request,HttpServletResponse response){
        request.getSession().setAttribute("loginUser",null);
        request.getSession().setAttribute("loginMrg",null);
        return "redirect:/showLogin.do";
    }

    @RequestMapping("/findAllUser")
    public String findAllUser(HttpServletRequest request,HttpServletResponse response){

        request.getSession().setAttribute("userSearchForm",new SearchForm());
        UserService userService = (UserService) ObjectFactory.getObject("UserService");
        User user= (User) request.getSession().getAttribute("loginUser");
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        if(pageNo==null){
            pageNo= PageConstant.PAGE_KAISHI_NO;
        }
        if(pageSize==null){
            pageSize=PageConstant.PAGE_SIZE;
        }
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            List<User> list = userService.findAllUser(user);
            PageInfo<User> PageInfo = new PageInfo<>(list);
            request.getSession().setAttribute("userListPage",PageInfo);
            request.getSession().setAttribute("Mrg",null);
            return  "/showUser.do";
        } catch (NoLoginException e) {
            e.printStackTrace();
            return  "redirect:/showLogin.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return  "redirect:/showHome.do";
        }
    }
    @ResponseBody
    @RequestMapping("/findUserById")
    public ResponseResult findUserById(HttpServletRequest request, HttpServletResponse response){
        UserService userService = (UserService) ObjectFactory.getObject("UserService");
        ResponseResult result=new ResponseResult();
        String id = request.getParameter("id");
        System.out.println("id="+id);
        try {
            User user=userService.findUserById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(user);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/modifyUserById")//该方法已作废
    public  ResponseResult modifyUserById(HttpServletRequest request,HttpServletResponse response){
        UserService userService = (UserService) ObjectFactory.getObject("UserService");
        ResponseResult result=new ResponseResult();
        String id = request.getParameter("id");
        System.out.println("id="+id);
        String username = request.getParameter("username");
        System.out.println("username="+username);
        String roleName = request.getParameter("roleName");
        System.out.println("roleName="+roleName);
        String password = request.getParameter("password");
        System.out.println("password="+password);
        String email = request.getParameter("email");
        System.out.println("email="+email);
        try {
            userService.modifyUserBy(id,username,roleName,password,email);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            return result;
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_PARAMETER_ERROR);
            return result;
        } catch (TpyeException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_PARAMETER_ERROR);
            return result;
        } catch (EmailExistException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_PARAMETER_ERROR);
            return result;
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            return result;
        }

    }
    @ResponseBody
    @RequestMapping("/modifyUserById2")
    public  ResponseResult modifyUserById2(HttpServletRequest request,HttpServletResponse response){
        UserService userService = (UserService) ObjectFactory.getObject("UserService");
        ResponseResult result=new ResponseResult();
        String id = request.getParameter("id");
        System.out.println("id="+id);
        String username = request.getParameter("username");
        System.out.println("username="+username);
        String roleName = request.getParameter("roleName");
        System.out.println("roleName="+roleName);
        String password = request.getParameter("password");
        System.out.println("password="+password);
        String email = request.getParameter("email");
        System.out.println("email="+email);
        try {
            userService.modifyUserBy2(id,username,roleName,password,email);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            return result;
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_PARAMETER_ERROR);
            return result;
        } catch (TpyeException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_PARAMETER_ERROR);
            return result;
        } catch (EmailExistException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_PARAMETER_ERROR);
            return result;
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            return result;
        }

    }
    @ResponseBody
    @RequestMapping("/modifyStatusById")
    public  ResponseResult modifyStatusById(HttpServletRequest request,HttpServletResponse response){
        UserService userService= (UserService) ObjectFactory.getObject("UserService");
        ResponseResult result=new ResponseResult();
        String status = request.getParameter("status");
        System.out.println("status="+status);
        String id = request.getParameter("id");
        System.out.println("id="+id);
        try {
            userService.modifyStatusById(id,status);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @RequestMapping("/sreachUserByForm")
    public  String sreachUserByForm(HttpServletRequest request,HttpServletResponse response){
        UserService userService = (UserService) ObjectFactory.getObject("UserService");
        String username = request.getParameter("username");
        System.out.println(username);
        String userNickname = request.getParameter("userNickname");
        System.out.println(userNickname);
        String email = request.getParameter("email");
        System.out.println( email);
        String roleName = request.getParameter("role-name");
        System.out.println(roleName);
        String kaishitime = request.getParameter("kaishitime");
        System.out.println(kaishitime);
        String jieshutime = request.getParameter("jieshutime");
        System.out.println(jieshutime);
        String kaishitime1 = request.getParameter("kaishitime1");
        System.out.println(kaishitime1);
        String jieshutime1 = request.getParameter("jieshutime1");
        System.out.println(jieshutime1);

        SearchForm searchForm = new SearchForm();
        searchForm.setUserLoginName(username);
        searchForm.setUserNickname(userNickname);
        searchForm.setUserEmil(email);
        searchForm.setUserRole(roleName);
        searchForm.setKaishitime(kaishitime);
        searchForm.setJiesutime(jieshutime);
        searchForm.setKaishitime2(kaishitime1);
        searchForm.setJieshutime2(jieshutime1);
        request.getSession().setAttribute("userSearchForm",searchForm);

        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        if(pageNo==null){
            pageNo= PageConstant.PAGE_KAISHI_NO;
        }
        if(pageSize==null){
            pageSize=PageConstant.PAGE_SIZE;
        }
        try {
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            List<User> list = userService.sreachUserByForm(username,userNickname,email,roleName,kaishitime,jieshutime,kaishitime1,jieshutime1);
            PageInfo<User> PageInfo = new PageInfo<>(list);
            request.getSession().setAttribute("userListPage",PageInfo);
            request.getSession().setAttribute("Mrg",null);
            return  "/showUser.do";
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/showUser.do";
        } catch (NullSringException e) {
            e.printStackTrace();
            request.getSession().setAttribute("userListPage",new PageInfo<User>());
            request.getSession().setAttribute("Mrg",e.getMessage());
            return "/showUser.do";
        }
    }
    @RequestMapping("/sreachUserByForm2")
    public String findResourceByForm2(HttpServletRequest request,HttpServletResponse response) {

        //获取之前的查询的条件以保持页面状态
        SearchForm userSearchForm = (SearchForm) request.getSession().getAttribute("userSearchForm");

        String userLoginName = userSearchForm.getUserLoginName();
        String userNickname = userSearchForm.getUserNickname();
        String userEmil = userSearchForm.getUserEmil();
        String userRole = userSearchForm.getUserRole();
        String kaishitime = userSearchForm.getKaishitime();
        String jiesutime = userSearchForm.getJiesutime();
        String kaishitime2 = userSearchForm.getKaishitime2();
        String jieshutime2 = userSearchForm.getJieshutime2();

        UserService userService = (UserService) ObjectFactory.getObject("UserService");
        String pageNo = null;//响应后显示的页数
        String pageNo1 = request.getParameter("pageNo");//get传过来的页数

        //pageNo2在第一次查询所有时没有意义，作用是用来保证通过别的按钮返回当前页时留在当前页
        PageInfo<Resource> Info = (PageInfo<Resource>) request.getSession().getAttribute("userListPage");
        String pageNo2 = "";
        if (Info != null) {
            pageNo2 = "" + Info.getPageNum();
        }

        //这个是表示在找到结果是如果数据改了，导致页面变少，自动跳转到前一页的页数
        String pageNo3 = (String) request.getSession().getAttribute("qianyiye");
        System.out.println("pageNo1=" + pageNo1);
        System.out.println("pageNo2=" + pageNo2);
        System.out.println("pageNo3=" + pageNo3);


        //以下为使用哪一个作为当前页数的判断
        if (pageNo3 != null) {
            System.out.println("用了前一页");
            pageNo = pageNo3;
        } else if (pageNo1 != null) {
            System.out.println("用了get");
            pageNo = pageNo1;
        } else if (!ParameterUtil.isNull(pageNo2)) {
            System.out.println("用了session");
            pageNo = pageNo2;
        } else {
            System.out.println("用了常量");
            pageNo = PageConstant.PAGE_KAISHI_NO;
        }

        String pageSize = request.getParameter("pageSize");
        if (pageSize == null) {
            pageSize = PageConstant.PAGE_SIZE;
        }
        List<User> list = null;
        try {
            PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
            list = userService.sreachUserByForm(userLoginName, userNickname, userEmil, userRole, kaishitime, jiesutime, kaishitime2, jieshutime2);
            PageInfo<User> pageInfo = new PageInfo<>(list);
            request.getSession().setAttribute("userListPage", pageInfo);
            request.getSession().setAttribute("Mrg",null);
            request.getSession().setAttribute("qianyiye", null);
            return "/showUser.do";
        } catch (NullSringException e) {
            if (pageNo.equals(PageConstant.PAGE_KAISHI_NO)) {
                //已经是第一页，表示为无满足条件的对象
                request.getSession().setAttribute("userListPage", new PageInfo<>());
                request.getSession().setAttribute("Mrg",e.getMessage());

                return "/showUser.do";
            } else {
                //设置前一页的页码放到session中
                String pageNum = "" + (Info.getPageNum() - 1);
                request.getSession().setAttribute("qianyiye", pageNum);
                return "/user/sreachUserByForm2.do";
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return "/showUser.do";

        }
    }
    @ResponseBody
    @RequestMapping("/jianchaUsername")
    public  ResponseResult jianchaUsername(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String nickname = request.getParameter("nickname");
        UserService userService = (UserService) ObjectFactory.getObject("UserService");
        try {
            userService.jianchaUsername(nickname);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/jianchaRoleName")
    public  ResponseResult jianchaRoleName(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String roleName = request.getParameter("roleName");
        UserService userService = (UserService) ObjectFactory.getObject("UserService");
        try {
            userService.jianchaRoleName(roleName);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (TpyeException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (NullSringException e) {
            e.printStackTrace();
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/jianchaEmail")
    public  ResponseResult jianchaEmail(HttpServletRequest request,HttpServletResponse response){
        ResponseResult result=new ResponseResult();
        String email = request.getParameter("email");
        String id=request.getParameter("id");
        UserService userService = (UserService) ObjectFactory.getObject("UserService");
        try {
            userService.jianchaEmail(email,id);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (TpyeException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (NullSringException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (ServiceException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        } catch (EmailExistException e) {
            e.printStackTrace();
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
